//
//  SearchViewController.m
//  GoBigMarket
//
//  Created by 李真 on 16/8/15.
//  Copyright © 2016年 legend. All rights reserved.
//  搜索页面。二期目标

#import "SearchViewController.h"
@interface SearchViewController ()

//热门搜索按钮底视图
@property (weak, nonatomic) IBOutlet UIView *hotBtnView;
@property (weak, nonatomic) IBOutlet UITableView *searchTableView;
//输入框文字
@property (copy, nonatomic) NSString *searchText;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"搜索";
    // Do any additional setup after loading the view from its nib.
    [_searchBar becomeFirstResponder];
//    [textField addTarget:self action:@selector(textEditingChanged) forControlEvents:UIControlEventEditingChanged];
    

}
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
}
//创建热词区域
- (UIView *)hotBtnView{
    
    return _hotBtnView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - searchDeleget

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{

    _searchBar.showsCancelButton = YES;
}
//输入结束
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    _hotBtnView.hidden = NO;
    _searchTableView.hidden = YES;
    _searchTableView.alpha = 0;
}
//输入修中
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    _hotBtnView.hidden = YES;
    _searchTableView.alpha = 1;
    _searchTableView.hidden = NO;
    
    _searchText = searchText;
    //当输入框没有文本时触发cancel按钮事件
    if (_searchText.length == 0) {
        [self searchBarCancelButtonClicked:searchBar];
    }
    
}
//cancel按钮触发 隐藏tableView 展示hotBtnView
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    _hotBtnView.hidden = NO;
    _searchTableView.hidden = YES;
    _searchTableView.alpha = 0;
    _searchBar.text = nil;
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *searchCell = [tableView dequeueReusableCellWithIdentifier:@"searchCell"];
    if (!searchCell) {
        searchCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"searchCell"];
    }
    searchCell.backgroundColor = [UIColor grayColor];
    searchCell.contentView.backgroundColor = [UIColor colorWithRed: ( arc4random() % 256 / 256.0 ) green: ( arc4random() % 256 / 256.0 ) blue: ( arc4random() % 256 / 256.0 ) alpha:1];
    return searchCell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    float cellheight = ( _searchTableView.frame.size.height ) / 5;
    return cellheight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed: ( arc4random() % 256 / 256.0 ) green: ( arc4random() % 256 / 256.0 ) blue: ( arc4random() % 256 / 256.0 ) alpha:1];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
